package com.iot.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidUtils {

    // 1.汉字：^[\u4e00-\u9fa5]{0,}$
    // 2.英文和数字：^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
    // 3.长度为3-20的所有字符：^.{3,20}$
    // 4.由26个英文字母组成的字符串：^[A-Za-z]+$
    // 5.由26个大写英文字母组成的字符串：^[A-Z]+$
    // 6.由26个小写英文字母组成的字符串：^[a-z]+$
    // 7.由数字和26个英文字母组成的字符串：^[A-Za-z0-9]+$
    // 8.由数字、26个英文字母或者下划线组成的字符串：^\w+$ 或 ^\w{3,20}$
    // 9.中文、英文、数字包括下划线：^[\u4E00-\u9FA5A-Za-z0-9_]+$
    // 10.中文、英文、数字但不包括下划线等符号：^[\u4E00-\u9FA5A-Za-z0-9]+$ 或
    // ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
    // 11.可以输入含有^%&',;=?$\"等字符：[^%&',;=?$\x22]+
    // 12.禁止输入含有~的字符：[^~\x22]
    public static boolean isValidInput(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[\u4E00-\u9FA5A-Za-z0-9-_./ 【】\\[\\],，：:'\"{}（）()”“\n\r、|?？。《》<>%#@]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidStrEngAndCn(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[\u4E00-\u9FA5A-Za-z0-9-/_]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidLetterAndNumber(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[A-Za-z0-9]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidJson(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[\u4E00-\u9FA5A-Za-z0-9-_./ 【】\\[\\],，：:'\"{}（）()”“、|?？《》<>%#@]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidLetterEn(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[A-Za-z]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }
    // 1.Email地址：^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
    // 2.域名：[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?
    // 3.InternetURL：[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
    // 4.身份证号(15位、18位数字)，最后一位是校验位，可能为数字或字符X：(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)
    // 5.帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)：^[a-zA-Z][a-zA-Z0-9_]{4,15}$
    // 6.密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)：^[a-zA-Z]\w{5,17}$
    // 7.强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 8-10
    // 之间)：^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$
    // 8.强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-10之间)：^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
    // 9.日期格式：^\d{4}-\d{1,2}-\d{1,2}
    // 10.一年的12个月(01～09和1～12)：^(0?[1-9]|1[0-2])$
    // 11.一个月的31天(01～09和1～31)：^((0?[1-9])|((1|2)[0-9])|30|31)$
    // 12.中国邮政编码：[1-9]\d{5}(?!\d) (中国邮政编码为6位数字)
    // 13.IPv4地址：((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}
    // 14.校验车牌号：^[粤湘鄂冀豫黑吉辽晋陕青鲁苏皖浙闽赣琼甘贵川港澳桂宁蒙新藏京津沪渝]{1}[A-HJ-NP-Z0-9]{6,7}$

    public static boolean isValidMail(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidAccountName(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidName(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[\u4E00-\u9FA5A-Za-z0-9-_./ 【】\\[\\],，：:'\"{}（）()”“\n\r]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidPasswd(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[a-zA-Z0-9_@#$%]\\w{5,17}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidUrl(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[A-Za-z0-9_/-:.]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }

    public static boolean isValidMobile(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        // String regex =
        // "^((13[0-9])|(14[05679])|(15([0-3,5-9]))|(16[2567])|(17[01235678])|(18[0-9]|19[135689]))\\d{8}$";
        // Pattern p = Pattern.compile(regex);
        // Matcher m = p.matcher(inputString);
        // boolean isMatch = m.matches();
        if (inputString.length() != 11) {
            return false;
        }
        int head2Char = Integer.parseInt(inputString.substring(0, 2));
        if ((head2Char < 13) || (head2Char > 19)) {
            return false;
        }
        return true;
    }

    public static boolean isValidDevId(String inputString) {
        if (inputString == null) {
            return false;
        }
        if (inputString.length() == 0) {
            return false;
        }
        String regex = "^[a-fA-F0-9]{12}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputString);
        boolean isMatch = m.matches();
        return isMatch;
    }
}
